<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    title="选择节点信息">
    <el-form :inline="true" :model="slaveList" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="slaveList.slaveId" clearable placeholder="节点id"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:user:delete')" :disabled="dataListSelections.length <= 0" type="primary"
                   @click="startUpHandle()">确认
        </el-button>
        <el-button v-if="isAuth('sys:user:delete')" :disabled="dataListSelections.length <= 0" type="primary"
                   @click="visible = false">取消
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle">
      <el-table-column
        align="center"
        header-align="center"
        type="selection"
        width="50">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="节点ID"
        prop="slaveId"
        width="80">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="待选择节点名称"
        prop="slaveName">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="ip地址"
        prop="ip">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="状态"
        prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">启用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="状况"
        prop="runFileId">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.runFileId === null" size="small" type="primary">空闲</el-tag>
          <el-tag v-else size="small" type="danger">正忙</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="权重(%)"
        prop="weight"
        width="180">
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle">
    </el-pagination>
  </el-dialog>
</template>

<script>


export default {
  name: "stressFile-slave",
  data() {
    return {
      visible: false,
      slaveList: {
        slaveId: '',
        status: 0
      },
      id: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      enableVisible: false
    }
  },
  activated() {
    this.getDataList()
  },
  methods: {
    //初始化 所有的数据
    init(fileId) {
      this.id = fileId
      this.$http({
        url: this.$http.adornUrl('test/stressSlave/list/enable'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          '_search': false,
        })

      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          this.visible = true
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('test/stressSlave/list/enable'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          '_search': false,
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 启用列表
    enableHandle(fileId) {
      this.enableVisible = true
      this.$nextTick(() => {
        this.$refs.enable.init(fileId)
      })
    },
    // 确认按钮
    startUpHandle(slaveId) {
      var slaveIds = slaveId ? [slaveId] : this.dataListSelections.map(item => {
        return item.slaveId
      })
      let fileId = [this.id]
      this.$confirm(`确定对[slaveId=${slaveIds.join(',')}]进行[${slaveId ? '启动' : '批量启动'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          // ${this.$cookie.get('token')}`
          url: this.$http.adornUrl('/test/stressFile/runOnce/' + slaveIds),
          method: 'post',
          data: this.$http.adornData(fileId, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: data.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
            this.visible = false
          } else {
            this.$message.error(data.msg)
            this.visible = false

          }
        })
      }).catch(() => {
        this.visible = false

      })
    },

  }
}
</script>
